package com.zkq.graduate.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zkq.graduate.pojo.SendMessageResult;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author zhangkaiqiang
 * @Date 2019/4/18  16:15
 * @Description 调用阿里大于进行发送短信的工具类
 */
@Slf4j
public class SendMessageUtil {

	private static final String LOGIN="login";
	private static final String REGISTER="register";
	private static final String UPDATE_PHONE="updatePhone";

	/**
	 * @Author zhangkaiqiang
	 * @param phoneNumber 要发送到的电话号码
	 * @param flag  用于区别登录注册或者更改手机号的模板
	 */
	public static String sendSms(String phoneNumber,String flag,int number){
		CommonResponse response=null;
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAILFFZxu0dJ2oi", "wRtLpGpqa4Ih2yLTtdLqng8v4NNebD");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		request.putQueryParameter("PhoneNumbers",phoneNumber);
		request.putQueryParameter("SignName","高校毕业设计管理系统");
		if(LOGIN.equals(flag)){
			request.putQueryParameter("TemplateCode","SMS_182405224");
		}else if(REGISTER.equals(flag)){
			request.putQueryParameter("TemplateCode","SMS_163845086");
		}else if(UPDATE_PHONE.equals(flag)){
			request.putQueryParameter("TemplateCode","SMS_164275767");
		}
		request.putQueryParameter("TemplateParam","{\"code\":"+number+"}");
		try {
			 response= client.getCommonResponse(request);
		} catch (ClientException e) {
			e.printStackTrace();
		}
		SendMessageResult sendMessageResult = JSON.parseObject(response.getData(), SendMessageResult.class);
		log.info("短信发送工具类>>>>>>,短信发送结果状态码:"+sendMessageResult.getCode());
		return response.getData();
	}

	/**
	 * @Author zhangkaiqiang
	 * @Description 用于生成六位验证码
	 * @Date 16:42 2019/4/18 
	 * @param 
	 * @return int
	 **/
	public static int makeRandomNumber(){

		return (int)((Math.random()*9+1)*100000);
	}

	/**
	 * @Author zhangkaiqiang
	 * @Description
	 * @Date 9:25 2019/4/19
	 * @param 
	 * @param phoneNumber
	 * @param date
	 * @param pageSize
	 * @param currentPage
	 * @return void
	 **/
	public static void querySendDetils(String phoneNumber,String date,int pageSize,int currentPage){
		DefaultProfile profile = DefaultProfile.getProfile("default", "LTAILFFZxu0dJ2oi", " wRtLpGpqa4Ih2yLTtdLqng8v4NNebD ");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("QuerySendDetails");
		request.putQueryParameter("PhoneNumber", phoneNumber);
		request.putQueryParameter("SendDate", date);
		request.putQueryParameter("PageSize", String.valueOf(pageSize));
		request.putQueryParameter("CurrentPage", String.valueOf(currentPage));
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}


	public static void main(String[] args) {
		sendSms("13054626536","login",123456);
	}
}
